﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_e_cartorder : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            //CommonBLL.RegisterCKFinder(CKEditorBody);
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {                
                LoadDataInfo(Request.QueryString["itemid"].ToString().Trim());                
            }
        }
    }    
    private void LoadDataInfo(string ItemID)
    {
        DataTable dt = new BLL.CmsBLL().CartOrder_ViewItemByID(ItemID);
        if (dt.Rows.Count > 0)
        {
            hdItemID.Value = dt.Rows[0]["OrderId"].ToString() ;
            string OrderId = dt.Rows[0]["OrderId"].ToString() ;
            txtSender.Text = dt.Rows[0]["Fullname"].ToString();
            txtEmail.Text = dt.Rows[0]["Email"].ToString();
            txtPhone.Text = dt.Rows[0]["PhoneNumber"].ToString();
            txt_OrderId.Text = OrderId;
            //CKEditorBody.Text = dt.Rows[0]["Body"].ToString(); 
            DataTable dtOrderDetail = new BLL.CmsBLL().GetOrderDetail(OrderId);
            if (dtOrderDetail != null && dtOrderDetail.Rows.Count > 0)
            {
                string statusId = dtOrderDetail.Rows[0]["StatusName"].ToString();
                RendeOrderDetail(dtOrderDetail);
                PopulateDropDown(statusId);
            }
        }
        else
            lblMessage.Text = "Load data failt!";
    }

    private void RendeOrderDetail(DataTable dtOrderDetail)
    {
        StringBuilder Html = new StringBuilder();
            Html.Append(                                
                "		<table id=\"dtcart\" class=\"carttable\">\n" +
                "			<tr>\n" +
                "				<th class=\"stt\">STT</th>\n" +
                "				<th class=\"name\">Sản phẩm</th>\n" +
                "				<th class=\"price\">Giá (vnđ)</th>\n" +
                "				<th class=\"sl\">Số lượng</th>\n" +
                "				<th class=\"total\">Thành tiền</th>\n" +
                "				<th class=\"function\">&nbsp;</th>\n" +
                "			</tr>\n"
                );
            double total = 0;
            string productUrl = string.Empty;
            int itemnumber = 0;
            double price = 0;
            string productCode = string.Empty;
            for (int i = 0; i < dtOrderDetail.Rows.Count; i++)
            {
                productCode = dtOrderDetail.Rows[i]["ProductID"].ToString();
                itemnumber = Convert.ToInt16(dtOrderDetail.Rows[i]["Quantity"]);
                price = Convert.ToDouble(dtOrderDetail.Rows[i]["Price"]);
                string ProductName = dtOrderDetail.Rows[i]["ProductName"].ToString();
                Html.Append(                    

                "			<tr class=\"odd\">\n" +
                "				<td class=\"stt\">" + (i + 1) + "</td>\n" +
                "				<td class=\"name\"><a href=\"" + productUrl + "\">" + ProductName + "</a></td>\n" +
                "				<td class=\"price\">" + price.ToString("#,###") + "</td>\n" +
                "				<td class=\"sl\">" + itemnumber + "</td>\n" +
                "				<td class=\"total\" >" + (price * itemnumber).ToString("#,###") + "</td>\n" +                
                "				</td>\n" +
                "			</tr>\n");
                total += (price * itemnumber);
            }
            Html.Append(
                "			<tr>\n" +
                "				<td class=\"totalsum\" colspan=\"6\">\n" +
                "					\n" +
                "					Tổng tiền: " + total.ToString("#,###") + " vnđ <br />\n" +
                "					\n" +
                "				</td>\n" +
                "			</tr>\n" +
                "		</table>\n");
        ltr_Orderdetail.Text = Html.ToString();
    }


    public void PopulateDropDown(string StatusName)
    {

        DataTable dtStatus = new DataTable();
        dtStatus = new BLL.CmsBLL().GetOrderStatus();
        //Fill drop down list
        drl_Status.DataSource = dtStatus;
        drl_Status.DataValueField = "StatusId";
        drl_Status.DataTextField = "StatusName";
        drl_Status.DataBind();
        drl_Status.Items.FindByText(StatusName).Selected = true;
        
    }

    protected void lbtEditItem_Click(object sender, EventArgs e)
    {
        try
        {
            new CmsBLL().UpdateOrderStatus(hdItemID.Value, int.Parse(drl_Status.SelectedValue)); //CartOrder_UpDateItem(int.Parse(hdItemID.Value), txtTitle.Text, CKEditorBody.Text + "", txtSender.Text + "", txtEmail.Text + "", txtPhone.Text + "");
            lblMessage.Text = "Cập nhật thành công!";            
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERROR! " + ex.Message.ToString();
        }
    }   
}